package labmanager.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import labmanager.utils.AccessDB;

import org.apache.log4j.Logger;

import dto.ResponseDTO;

public class LabManagerReservation {

	public static AccessDB mysql = new AccessDB();
	private static final Logger LOGGER = Logger.getLogger(LabManagerReservation.class.getName());
	
	/**
	 * @param dateReservation
	 * @param typeReservation
	 * @param timeReservation
	 * @param userId
	 * @return
	 */
	public ResponseDTO createReservation(String dateReservation,
			int typeReservation, int timeReservation, String state, int userId) {
			
		LOGGER.debug(new StringBuilder("Invocando al metodo :: createReservation :: Parametros recibidos: dateReservation: ")
				.append(dateReservation).append(", typeReservation: ").append(typeReservation)
				.append(", timeReservation: ").append(timeReservation).append(", userId: ").append(userId).toString());
		ResponseDTO response = new ResponseDTO();
		try {
			response.setCode(98);
			response.setMessage("No se ha podido registrar la reserva.");
			Date now = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			StringBuilder query = new StringBuilder("INSERT INTO reservations (date_reservation, type_reservation, time_reservation, state, user_id, created_date) VALUES ('")
						.append(dateReservation).append("', ").append(typeReservation).append(", ").append(timeReservation).append(", 'Pendiente', ")
						.append(userId).append(", '").append(format.format(now)).append("')");
				int resultDML = mysql.executeQueryDML(query.toString());
				if (resultDML != 0) {
					response.setCode(0);
					response.setMessage("Reserva registrada correctamente.");
				}
			LOGGER.debug(new StringBuilder("Respuesta al portal:").append(response.getMessage()).toString());
			return response;
		} catch (Exception e) {
			LOGGER.debug(new StringBuilder("Se produjo un error en database/LabManagerReservation al registrar la reserva en base de datos. ").append(e).toString(), e);
			response.setCode(99);
			response.setMessage("Se ha producido un error.");
			return response;
		}
	}
	
	/**
	 * @param id
	 * @param dateReservation
	 * @param typeReservation
	 * @param timeReservation
	 * @param state
	 * @return
	 */
	public ResponseDTO updateReservation(int id, String dateReservation,
			int typeReservation, int timeReservation, String state) {
		
		LOGGER.debug(new StringBuilder("Invocando al metodo :: updateReservation :: Parametros recibidos: dateReservation: ").append(dateReservation)
				.append(", typeReservation: ").append(typeReservation).append(", timeReservation: ").append(timeReservation).append(", state: ")
				.append(state).toString());
		ResponseDTO response = new ResponseDTO();
		try {
			StringBuilder query = new StringBuilder("SELECT * FROM reservations WHERE id = ").append(id);
			ResultSet result = mysql.executeQuery(query.toString());
			if (result.next()) {
				if(dateReservation.equals("")){
					query = new StringBuilder("UPDATE reservations SET state = '").append(state)
							.append("' WHERE id =").append(id);
				}else{
					query = new StringBuilder("UPDATE reservations SET date_reservation = '").append(dateReservation).append("', type_reservation = ")
							.append(typeReservation).append(", time_reservation = ").append(timeReservation)
							.append("' WHERE id =").append(id);
				}
				int resultDML = mysql.executeQueryDML(query.toString());
				if (resultDML != 0) {
					response.setCode(resultDML);
					response.setMessage("La reserva del usuario ha sido actualizado correctamente.");
				}
			} else {
				response.setCode(97);
				response.setMessage("La reserva no existe en la base de datos.");
			}
		} catch (SQLException e) {
			LOGGER.debug(new StringBuilder("Se produjo un error en database/updateReservation al actualizar la reserva en base de datos. ")
					.append(e).toString(), e);
			response.setCode(99);
			response.setMessage("Se ha producido un error.");
			return response;
		}
		return response;
	}

}
